package com.zhuiyun.project.api.studentTraining.service.impl;


import com.zhuiyun.project.api.memberUserManagement.entity.MemberUserManagement;
import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
import com.zhuiyun.project.api.reservation.entity.Reservation;
import com.zhuiyun.project.api.reservation.mapper.ReservationMapper;
import com.zhuiyun.project.api.studentTraining.model.MemberUserInfoModel;
import com.zhuiyun.project.api.studentTraining.service.StudentTrainingService;
import com.zhuiyun.project.api.trainingRecord.entity.TrainingRecord;
import com.zhuiyun.project.api.trainingRecord.mapper.TrainingRecordMapper;
import com.zhuiyun.project.common.response.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.*;

/**
 * @ClassName StudentTrainingServiceImpl
 * @Description TODO 学员端训练页面管理接口实现
 * @Author wx
 * @Date 2023/11/20 19:05
 **/
@Service
public class StudentTrainingServiceImpl implements StudentTrainingService {
    @Autowired
    private ReservationMapper reservationMapper;

    @Autowired
    private TrainingRecordMapper trainingRecordMapper;

    @Autowired
    private MemberUserManagementMapper memberUserManagementMapper;

    /**
     * @Author wx
     * @Description 获取会员用户当日训练时间
     * @Date 2023/11/20 19:09
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingTime(Integer id, String venueUniqueIdentifier) {
        List<Reservation> reservationList = reservationMapper.getStudentTrainingAllTime(id,venueUniqueIdentifier);
        List<Reservation> reservationList1 = reservationMapper.getStudentTrainingFinishTime(id,venueUniqueIdentifier);
        int totalCourseTime1 = 0;
        int totalCourseTime2 = 0;

        if (reservationList != null && !reservationList.isEmpty()) {
            for (Reservation reservation : reservationList) {
                totalCourseTime1 += Integer.parseInt(reservation.getCourseTime());
            }
        }

        if (reservationList1 != null && !reservationList1.isEmpty()) {
            for (Reservation reservation : reservationList1) {
                totalCourseTime2 += Integer.parseInt(reservation.getCourseTime());
            }
        }

        // 将累加后的总课程时长封装到返回结果中
        Map<String, Object> result = new HashMap<>();
        result.put("totalCourseTime1", totalCourseTime1);
        result.put("totalCourseTime2", totalCourseTime2);

        System.out.println(result);
        return  CommonResult.ok(result);
    }

    /**
     * @Author wx
     * @Description 获取会员用户体重记录
     * @Date 2023/11/21 8:55
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingWeight(Integer id, String venueUniqueIdentifier) {
        TrainingRecord trainingRecord = trainingRecordMapper.getStudentTrainingWeight(id,venueUniqueIdentifier);
        Double userWeright = 0.00;
        if (trainingRecord == null){
            MemberUserManagement memberUserManagement = memberUserManagementMapper.getStudentTrainingWeight(id,venueUniqueIdentifier);
            userWeright = memberUserManagement.getMemberUserWeight();

        }else {
            userWeright=trainingRecord.getUserWeight();
        }
        return CommonResult.ok(userWeright);
    }

    /**
     * @Author wx
     * @Description 获取会员用户所有体重记录
     * @Date 2023/11/21 9:22
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingAllWeight(Integer id, String venueUniqueIdentifier) {
        List<TrainingRecord> trainingRecordList = trainingRecordMapper.getStudentTrainingAllWeight(id,venueUniqueIdentifier);
        return CommonResult.ok(trainingRecordList);
    }

    /**
     * @Author wx
     * @Description 获取会员用户前天最近一次体重记录
     * @Date 2023/11/21 8:55
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingLastWeight(Integer id, String venueUniqueIdentifier) {
        TrainingRecord trainingRecord = trainingRecordMapper.getStudentTrainingLastWeight(id,venueUniqueIdentifier);
        Double userWeright = 0.00;
        if (trainingRecord == null){
            MemberUserManagement memberUserManagement = memberUserManagementMapper.getStudentTrainingWeight(id,venueUniqueIdentifier);
            userWeright = memberUserManagement.getMemberUserWeight();

        }else {
            userWeright=trainingRecord.getUserWeight();
        }
        return CommonResult.ok(userWeright);
    }

    /**
     * @Author wx
     * @Description 查询近六个月的体重数据
     * @Date 2023/11/21 14:16
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingSixWeight(Integer id, String venueUniqueIdentifier) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 存储数字和对应年份的数组
        Integer[] months = new Integer[6];
        Integer[] years = new Integer[6];

        // 逐个向前计算并存储数字和对应年份
        for (int i = 5; i >=0; i--) {
            // 存储月份和对应的年份
            months[i] = currentDate.getMonthValue();
            years[i] = currentDate.getYear();

            // 将日期向前推移一个月
            currentDate = currentDate.minusMonths(1);
        }

        // 定义数组，用于存储每次查询到的数据
        Double[] minWeights = new Double[6];
        Double[] maxWeights = new Double[6];
        Integer lastMonth = months[0];
        Integer lastYear =years[0];
        TrainingRecord trainingRecord = trainingRecordMapper.getStudentTrainingMinWeight(lastMonth, lastYear, id);
        Double weight = 0.0;
        if (trainingRecord ==null) {
            MemberUserManagement memberUserManagement = memberUserManagementMapper.getStudentTrainingWeight(id,venueUniqueIdentifier);
            weight=memberUserManagement.getMemberUserWeight();
        }else {
            weight =trainingRecord.getUserWeight();
        }
        // 遍历数组，传递月份和对应年份调用其他方法
        for (int i = 0; i < 6; i++) {
            int month = months[i];
            int year = years[i];

            // 调用其他方法，传递月份和对应年份作为参数，并将返回结果保存到数组中
            TrainingRecord trainingRecordList = trainingRecordMapper.getStudentTrainingMinWeight(month, year, id);
            if (trainingRecordList != null && trainingRecordList.getUserWeight() != null) {
                minWeights[i] = trainingRecordList.getUserWeight();
            } else {
                if (i==0){
                    minWeights[0] =weight;
                }else {
                    minWeights[i] = minWeights[i-1];
                }
            }

            TrainingRecord trainingRecordList1 = trainingRecordMapper.getStudentTrainingMaxWeight(month, year, id);
            if (trainingRecordList1 != null && trainingRecordList1.getUserWeight() != null) {
                maxWeights[i] = trainingRecordList1.getUserWeight();
            } else {
                if (i==0){
                    maxWeights[0] =weight;
                }else {
                    maxWeights[i] = maxWeights[i-1];
                }

            }
        }

//        // 将数组转换为List类型，并使用Collections.reverse()方法反转元素顺序
//        List<Double> minWeightsList = Arrays.asList(minWeights);
//        List<Double> maxWeightsList = Arrays.asList(maxWeights);
//        List<Integer> monthsList = Arrays.asList(months);
//        Collections.reverse(minWeightsList);
//        Collections.reverse(maxWeightsList);
//        Collections.reverse(monthsList);
//
//        // 创建新的数组并将反转后的元素填充到新数组中
//        Double[] reversedMinWeights = new Double[minWeightsList.size()];
//        Double[] reversedMaxWeights = new Double[maxWeightsList.size()];
//        Integer[] reversedMonths = new Integer[monthsList.size()];
//        for (int i = 0; i < reversedMinWeights.length; i++) {
//            reversedMinWeights[i] = minWeightsList.get(i);
//            reversedMaxWeights[i] = maxWeightsList.get(i);
//            reversedMonths[i] = monthsList.get(i);
//        }
//        Map<String, Object> resultMap = new HashMap<>();
//        resultMap.put("minWeights", reversedMinWeights);
//        resultMap.put("maxWeights", reversedMaxWeights);
//        resultMap.put("months", reversedMonths);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("minWeights", minWeights);
        resultMap.put("maxWeights", maxWeights);
        resultMap.put("months", months);
        // 返回CommonResult对象
        return CommonResult.ok(resultMap);
    }

    /**
     * @Author wx
     * @Description 查询月训练比例
     * @Date 2023/11/22 8:36
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentTrainingMonth(Integer id, String venueUniqueIdentifier) {
        List<Reservation> reservationList = reservationMapper.getStudentTrainingMonth(id);
        if (reservationList.size() != 0) {
            String[] timeArray = new String[reservationList.size()];
            int[] resultArray = new int[reservationList.size()];

            for (int i = 0; i < reservationList.size(); i++) {
                Reservation reservation = reservationList.get(i);
                String reservationTime = reservation.getReservationTime();
                int reservationStatus = Integer.parseInt(reservation.getReservationStatus());

                // 提取日期中的天数
                String day = reservationTime.substring(8, 10);
                timeArray[i] = day+"日";

                if (reservationStatus == 1) {
                    resultArray[i] = 0;
                } else if (reservationStatus == 2) {
                    resultArray[i] =  50;
                } else if (reservationStatus == 3) {
                    resultArray[i] = 100;
                }
            }
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("timeArray", timeArray);
            resultMap.put("resultArray", resultArray);
            System.out.println(resultMap);
            return CommonResult.ok(resultMap);
            // 将结果数组使用适当的方式进行处理

        } else {
            return CommonResult.ok(null);
        }
    }


    /**
     * @Author wx
     * @Description 查询今日训练消耗
     * @Date 2023/11/28 9:10
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getStudentNowkcal(Integer id, String venueUniqueIdentifier) {
        List<TrainingRecord> trainingRecordList = trainingRecordMapper.getStudentNowkcal(id,venueUniqueIdentifier);
        Double nowkcal=0.00;
        if (trainingRecordList.size()>0){
            for (TrainingRecord record : trainingRecordList) {
                nowkcal += record.getEnergyConsumption();
            }
        }
        return CommonResult.ok(nowkcal);
    }


    /**
     * @Author wx
     * @Description 查询会员用户信息
     * @Date 2023/11/28 10:14
     * @Return
     * @param venueUniqueIdentifier
     */
    @Override
    public CommonResult getUserInformation(Integer id, String venueUniqueIdentifier) {
        MemberUserInfoModel memberUserInfoModel =memberUserManagementMapper.getUserInformation(id,venueUniqueIdentifier);
        return CommonResult.ok(memberUserInfoModel);
    }
}
